<?php

namespace app\admin\controller;

use app\index\model\data;
use think\Controller;
use think\Request;
use think\Validate;///验证类库

class Index extends Controller
{
    public function __construct(Request $request = null)
    {

        if( !session('admin') || session('admin')!='123'){
            echo "<script>
//                        alert('对不起您并未登录用户！');
//                        setTimeout(function(){
                            location.href='/index.php/admin/login';
//                        },500)
                        </script>";
            exit();
        }else{
            //think实例化方法
//            $log= new Log();
//            $log->log_xie('登录系统');
            $this->log= Controller('Log');

        }
        parent::__construct($request);
    }
    //首页的嵌套首页
    public function sy(){
        return $this->fetch();
    }
    ///20后台首页
     public function index(){
         $ip=request()->ip();

//        var_dump($ip);
         $qy_id=session('qy_id');
         $dc=new Data();
         $qy_name=$dc->d_qqy($qy_id);
         if(empty($qy_name)){
             $this->error('您所在的区域还未开通本系统服务，请联系“廊坊鸿星志金网络科技有限公司”');
         }
         session('ip',$ip);
//         session('qy_id',$qy_id);
//        var_dump($qy_name);
         $new=$dc->d_qwuzi($qy_id);
         $zjsum=$dc->d_qzjsum($qy_id);
         $wzsum=$dc->d_qwzsum($qy_id);
         $this->assign('qy_id',json_encode($qy_id));
         $this->assign('zjsum',json_encode($zjsum));
         $this->assign('wzsum',json_encode($wzsum));
         $this->assign('list',json_encode($new));

         $this->assign('qy_name',json_encode($qy_name));

         return $this->fetch();
    }
    /////物资审核通过，撤销操作
    public function wzcz(){
        $cood=$_POST['cood'];
        $wz_id=$_POST['wz_id'];
        $cd=new Data();
        if($cood=='d_wzcx'){
            $new=$cd->d_wzcx($wz_id);
            if($new==-1){
                $this->error('操作失败！请检查网络！');
            }else{
                $this->success('物资撤销操作成功！');
            }
        }
        if($cood=='d_wztg'){
            $new=$cd->d_wztg($wz_id);
            if($new==-1){
                $this->error('操作失败！请检查网络！');
            }else{
                $this->success('物资通过操作成功！');
            }
        }
    }
    //礼品网首页
    public function tggl(){
        $ks=db('ks')->where('str=1 and lx_id=1')->select();
        $sql="
            SELECT
            y.*,
            k.name kname
            FROM
            yuyue y LEFT JOIN ks k ON y.ks_id=k.id
            ORDER BY k_time desc limit 10
        ";
        $new=db()->query($sql);
        $this->assign('yuyue',json_encode($new));
        $q=db('yuyue')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $this->assign('q_y',json_encode($q_y));
        $this->assign('g',json_encode($q));
        $this->assign('ks',json_encode($ks));
        return $this->fetch();
    }
    //创建预约视图
    public function cjyy(){
        $new=db('ks')->where('str=1 and lx_id=1')->select();
        $this->assign('ks',json_encode($new));
        return $this->fetch();
    }
    //修改礼品数据《《《
    public function xglipin()
    {
        $id=$_POST['id'];
        $data['name']=$_POST['name'];
        $data['kucun']=$_POST['kucun'];
        $data['danjia']=$_POST['danjia'];
        $data['text']=$_POST['text'];
        $data['imgurl']=$_POST['imgurl'];
        $data['e_time'] = time();
        $new=db('lp_lipin')->where('id',$id)->update($data);
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('修改了礼品数据 -'.$data['name'].'- 的信息');
            echo 1;
        }
    }
    //删除预约/门牌数据 单一或者批量
    public function scyuyue()
    {
        $id=$_POST['id'];
        $d=$_POST['d'];//是否批量操作
        $db=$_POST['db'];//数据库名称
        if($d=='pl'){
            $new=db($db)->delete($id);
        }else{
            $new=db($db)->where('id',$id)->delete();
        }
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('删除了 '.$db.' 数据 -'.json_encode($id).'- 的信息');
            echo 1;
        }
    }



    //创建预约控制器
    public function cjyuyue(){
        $data['name']=$_POST['name'];
        $data['j_name']=$_POST['j_name'];
        $data['y_name']=$_POST['y_name'];
        $data['ks_id']=$_POST['ks_id'];
        $data['gaishu']=$_POST['gaishu'];
        $data['time']=$_POST['time'];
        $data['k_time']=time();
        $isname=db('yuyue')->where('name',$data['name'])->find();
        if(!empty($isname)){
            echo -2;
            exit();
        }
        $new=db('yuyue')->insert($data);
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('创建了 预约 数据 -'.$data['name'].'- 的信息');
            echo 1;
        }
    }
    //门牌列表
    public function mplb(){
        $sql="
                SELECT
            m.*,
            k.name kname,
            mb.name mbname
            FROM
           menpai m LEFT JOIN ks k ON m.ks_id=k.id
           LEFT JOIN menpai_moban mb ON mb.id=m.mb_id
            ORDER BY k_time desc limit 10
        ";
        $new=db()->query($sql);

        $q=db('menpai')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $ks=db('ks')->where('str!=2')->order('k_time desc')->select();//ks数据10条
        $this->assign('q_y',json_encode($q_y));
        $this->assign('ks',json_encode($ks));
        $this->assign('g',json_encode($q));

        $mb=db('menpai_moban')->where('str',1)->order('k_time desc')->select();
        $this->assign('moban',json_encode($mb));
        $this->assign('mp',json_encode($new));
        return $this->fetch();
    }
    //创建门牌视图
    public function cjmp(){
        $new=db('ks')->where('str',1)->select();
        $moban=db('menpai_moban')->where('str',1)->select();
        $this->assign('ks',json_encode($new));
        $this->assign('moban',json_encode($moban));
        return $this->fetch();
    }
    //创建礼品操作
    public function czcjmp(){
        $data['text']=$_POST['text'];
        $data['imgurl']=$_POST['imgurl'];
        $data['name']=$_POST['name'];
//        $data['videourl']=$_POST['videourl'];
        $data['kucun']=$_POST['kucun'];
        $data['danjia']=$_POST['danjia'];

        $data['xs']=$_POST['xs'];
        $data['k_time']=time();//添加这个礼品的时间
//        var_dump($data);

        $new=db('lp_lipin')->insert($data);
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('创建了 礼品 数据 -'.$data['text'].'- 的信息');
            echo 1;
        }
    }
    //cz——修改门牌操作
    public function cz_xgmp(){
        $id=$_POST['id'];
        $data['text']=$_POST['text'];
        $data['imgurl']=$_POST['imgurl'];
        $data['videourl']=$_POST['videourl'];
        $data['ks_id']=$_POST['ks_id'];
        $data['mb_id']=$_POST['mb_id'];
        $data['xs']=$_POST['xs'];
        $data['e_time']=time();
        $new=db('menpai')->where('id',$id)->update($data);
        if(empty($new)){
            echo -1;
        }else{
            $this->log->log_xie('修改了 门牌 数据 -'.json_encode($id).'- 的信息');
            echo 1;
        }
    }


    //终端管理页面
    public function zdgl(){
        $q=db('zhongduan')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $ks=db('ks')->where('str',1)->select();
        $this->assign('q_y',json_encode($q_y));
        $this->assign('ks',json_encode($ks));
        $this->assign('g',json_encode($q));
        $sql="
            SELECT 
            z.*,
            k.name kname
            FROM
           zhongduan z LEFT JOIN ks k ON z.ks_id=k.id
            ORDER BY k_time desc limit 10 
        ";
        $new=db()->query($sql);
//        $new=db('zhongduan')->where('str',1)->limit(10)->select();
        $this->assign('zd',json_encode($new));
        return $this->fetch();
    }
    //创建终端页面
    public function cjzd(){

        $new=db('ks')->where('str',1)->select();
        $this->assign('ks',json_encode($new));
        return $this->fetch();
    }
    ///创建终端数据控制器
    public function cz_cjzd(){
        $data['ip']=$_POST['name'];
        $validate = new Validate([
            'ip' => 'ip'
        ]);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        if($data['ip']==''){
            $this->error('操作失败！请填入ip');
        }
        $isname=db('zhongduan')->where('ip',$data['ip'])->find();
        if(!empty($isname)) {
            $this->error('重复的终端ip！');
        }
        ///验证ip地址的格式是否正确？


        $data['str']=$_POST['pid'];
        $data['ks_id']=$_POST['ks_id'];
        $data['k_time']=time();
        $new=db('zhongduan')->insert($data);
        if(empty($new)){
            $this->error('操作失败！网络错误');
        }else{
            $this->log->log_xie('创建终端 数据 -'.$data['ip'].'- 的信息');
            $this->success('添加终端成功！', 'zdgl');
        }
    }
    /////修改终端
    public function cz_upzd(){
        $data['ip']=$_POST['name'];
        $validate = new Validate([
            'ip' => 'ip'
        ]);
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        if($data['ip']==''){
            $this->error('操作失败！请填入ip');
        }

        ///验证ip地址的格式是否正确？

        $data['Id']=$_POST['id'];
        $data['str']=$_POST['pid'];
        $data['ks_id']=$_POST['ks_id'];
        $new=db('zhongduan')->where('id',$data['Id'])->update($data);
        if(empty($new)){
            $this->error('操作失败！网络错误');
        }else{
            $this->log->log_xie('修改终端 数据 -'.$data['ip'].'- 的信息');
            $this->success('更改终端成功！', 'zdgl');
        }
    }
    //报表查询->预约查询视图
    public function yycx(){
        $q=db('yuyue')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $new=db('yuyue')->limit(10)->select();
        $sql="
            SELECT
            y.*,
            k.name kname
            FROM
            yuyue y LEFT JOIN ks k ON y.ks_id=k.id
            ORDER BY k_time desc limit 10
        ";
        $new=db()->query($sql);


        $this->assign('q_y',json_encode($q_y));
        $this->assign('yuyue',json_encode($new));
        $this->assign('g',json_encode($q));
        return $this->fetch();
    }
    //报表查询->调解室查询视图
    public function tjscx(){
        $q=db('ks')->count();//所有数据
        if($q<=10){
            $q_y=1;
        }else{
            $q_y=floor($q/10)+1;//去掉整数返回除以10再+1的页数
        }
        $new=db('ks')->select();
//        $sql="
//            SELECT
//            y.*,
//            k.name kname
//            FROM
//            yuyue y LEFT JOIN ks k ON y.ks_id=k.id
//            ORDER BY k_time desc limit 10
//        ";
//        $new=db()->query($sql);

        $this->assign('q_y',json_encode($q_y));
        $this->assign('ks',json_encode($new));
        $this->assign('g',json_encode($q));
        return $this->fetch();


        return $this->fetch();
    }

    //上一页下一页尾页
    public function page(){
        //当前页面
        $y=$_POST['y'];
        //要开始获取的位置
        $w=$y*10-10;
        $d=$_POST['d'];
        //d的索引数据库
//        $new=db($d)->order('k_time desc')->limit($w,10)->select();
        $sql="
            SELECT
            y.*,
            k.name kname
            FROM
            $d y LEFT JOIN ks k ON y.ks_id=k.id
            ORDER BY k_time desc limit $w,10
        ";
        $new=db()->query($sql);
        if(empty($new)){
            echo -1 ;
        }else{
            echo json_encode($new) ;
        }

    }
    /////报表查询->预约查询->日期查询操作
    public function rqcx(){

        $time=$_POST['t'];
        $time=$time/1000;
        $d=$_POST['d'];
        if($d=='yuyue'){
//            $new=db('yuyue')->where($time.'<e_time')->select();
            $sql="
            SELECT
            y.*,
            k.name kname
            FROM
            yuyue y LEFT JOIN ks k ON y.ks_id=k.id
            WHERE $time<y.time
            ORDER BY k_time desc
        ";
            $new=db()->query($sql);
        }
        if(empty($new)){
            echo -1 ;
        }else{
            echo json_encode($new) ;
        }

    }
    /////报表查询->科室查询->日期查询操作
    public function ks_rqcx(){

        $time=$_POST['t'];
        $time=$time/1000;
        $d=$_POST['d'];
        if($d=='ks'){
            $new=db('ks')->where($time.'<e_time')->select();
        }
        if(empty($new)){
            echo -1 ;
        }else{
            echo json_encode($new) ;
        }

    }
}